<?php
/**
 * 阿里
 * Created by PhpStorm.
 * User: ZengYou
 * Date: 2018/10/13 0013
 * Time: 12:45
 */
namespace app\common\validate;

class Alibaba extends BaseValidate
{

    /**
     * 检查是否已过期
     */
    protected function checkExpired($value, $rule, $data)
    {
        $redisName = 'admin' . $data['key'] . $data['phone'];

        $ttl = app('redisController')->ttl($redisName);

        if ($ttl !== -2 && $ttl > 240) exception('请不要重复获取验证码');

       return true;
    }

    protected $regex = [];

    protected $rule = [
        'phone'     => 'require|max:20',
        'key'       => 'require|in:registered,login|checkExpired',
    ];

    protected $message = [
        'phone.require'        => '手机号码不能为空',
        'phone.max'            => '手机号码长度不能超出20位',
        'key.require'          => 'Key不能为空',
        'key.max'              => 'Key长度不能选项内',
    ];

    /**
     * 添加
     */
    protected function sceneSendSms()
    {
        return $this->only([
            'phone', 'key'
        ]);
    }

}